// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Restore building / compositions that have been backup
//             Position is the center of composition
// -----------------------------------------------

private [
	"_soldier",
	"_pos",
	"_xpos",
	"_ypos",
	"_zpos",
	"_dir",
	"_typeof",
	"_building",
	"_position",
	"_arrayof",
	"_withmen",
	"_group"
];

_position = _this select 0;
_arrayof  = _this select 1;
_withmen  = _this select 2;

_group = createGroup east;
{
	_pos = _x select 0;
	_xpos = (_pos select 0) + (_position select 0);
	_ypos = (_pos select 1) + (_position select 1);
	_zpos = _pos select 2;
	_pos = [_xpos, _ypos, _zpos];
	_dir = _x select 1;
	_typeof = _x select 2;
	_building = createVehicle [_typeof, _pos, [], 0, "NONE"];
	_building setPosATL _pos;
	_building setDir _dir;
	_building setVectorUp [0,0,1];

	//A3 TODO: Buildings; Is this even used?
	if (_typeof == "TK_WarfareBHeavyFactory_Base_EP1") then {
		wcheavyfactory = _building;
	};

	if (_typeof == "Land_Barrack2_EP1") then {
		wcbarrack = _building;
	};

	if (_typeof == "Land_Ind_IlluminantTower") then {
		wclight = _building;
		["wclight", "client"] call WC_fnc_publicvariable;
	};

	if (_withmen) then {
		if (_building emptyPositions "driver" > 0) then {
			_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), [100,100,0], [], 0, "NONE"];
			_soldier assignAsDriver _building;
			_soldier moveInDriver _building;
			wcgarbage = [_soldier, wcside] spawn WC_fnc_sentinelle;
			wcgarbage = [_soldier, wcskill] spawn WC_fnc_setskill;
			_soldier addEventHandler ["Killed", {
				wcgarbage = _this spawn WC_fnc_garbagecollector;
				 wcenemykilled = wcenemykilled + 1;
				["wcenemykilled", "client"] call WC_fnc_publicvariable;
			}];
			_soldier addEventHandler ["Hit", {
				(_this select 0) doTarget (_this select 1);
				(_this select 0) doFire (_this select 1);
			}];
			wcunits = wcunits + [_soldier];
			wcblinde = wcblinde + [_soldier];
		};
		if (_building emptyPositions "gunner" > 0) then {
			_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), [100,100,0], [], 0, "NONE"];
			_soldier assignAsGunner _building;
			_soldier moveInGunner _building;
			wcgarbage = [_soldier, wcside] spawn WC_fnc_sentinelle;
			wcgarbage = [_soldier, wcskill] spawn WC_fnc_setskill;
			_soldier addEventHandler ["Killed", {
				wcgarbage = _this spawn WC_fnc_garbagecollector;
				wcenemykilled = wcenemykilled + 1;
				["wcenemykilled", "client"] call WC_fnc_publicvariable;
			}];
			_soldier addEventHandler ["HandleDamage", {
				(_this select 0) doTarget (_this select 3);
				(_this select 0) doFire (_this select 3);
				(_this select 0) reveal (_this select 3);
				(_this select 0) setDamage (0.5 + (random 0.5));
			}];
			wcunits = wcunits + [_soldier];
			wcblinde = wcblinde + [_soldier];
		};
		if (_building emptyPositions "commander" > 0) then {
			_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), [100,100,0], [], 0, "NONE"];
			_soldier assignAsCommander _building;
			_soldier moveInCommander _building;
			wcgarbage = [_soldier, wcside] spawn WC_fnc_sentinelle;
			wcgarbage = [_soldier, wcskill] spawn WC_fnc_setskill;
			_soldier addEventHandler ["Killed", {
				wcgarbage = _this spawn WC_fnc_garbagecollector;
				wcenemykilled = wcenemykilled + 1;
				["wcenemykilled", "client"] call WC_fnc_publicvariable;
			}];
			_soldier addEventHandler ["Hit", {
				(_this select 0) doTarget (_this select 1);
				(_this select 0) doFire (_this select 1);
			}];
			wcunits = wcunits + [_soldier];
			wcblinde = wcblinde + [_soldier];
		};
	};
	_building addEventHandler ["Killed", {wcgarbage = _this spawn WC_fnc_garbagecollector}];
	sleep 0.1;
	wcobjecttodelete = wcobjecttodelete + [_building];
} foreach _arrayof;

//if (count (units _group) > 0) then {
// initialisation script for units
//	_markername = format ["wccomposition%1", wccomposition];
//	_markersize = 50;
//	_marker = [_markername, _markersize, _position, "ColorBlue", "ICON", "FDIAGONAL", "EMPTY"] call WC_fnc_createmarker;
//	_scriptinit = format ["wcgarbage = [this, '%1', 'noslow'] execVM 'extern\ups.sqf';", _marker];
//	_leader = leader _group;
//	_leader setVehicleInit _scriptinit;
//	wccomposition = wccomposition + 1;
//};
//_group;